<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <th:block th:insert="common/fragment :: icon"></th:block>
    <title>添加博客</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
    <!-- markdown编辑插件 -->
    <link rel="stylesheet" href="../../static/lib/editormd/css/editormd.min.css"
          th:href="@{/lib/editormd/css/editormd.css}"/>
    <link rel="stylesheet" href="../../static/css/me.css" th:href="@{/css/me.css}">
</head>

<body style="background-color: white; background-image: none;">
    <!-- 导航栏部分 -->
    <nav th:replace="common/admin-frag :: firstmenu(1)"></nav>
    <!-- 二级导航 -->
    <div th:replace="common/admin-frag :: secondmenu(1,~{::#a1},~{::#a2})">
        <a id="a1" href="#" th:href="@{/admin/blogs/input}" class="item" th:classappend="${n==1} ? 'teal active'">发布</a>
        <a id="a2" href="#" th:href="@{/admin/blogs}" class="item" th:classappend="${n==2} ? 'teal active'">列表</a>
    </div>

    <!-- 中间内容部分 -->
    <div class="">
        <div class="" style="margin-bottom: 1em;">
            <form id="blog-form" action="#" th:object="${blog}" th:action="@{/admin/blogs}" method="POST" class="ui form">
                <input type="hidden" name="published" th:value="*{published}">
                <input type="hidden" name="id" th:value="*{id}">
                <div class="required field" style="margin-bottom: 0;">
                    <div class="ui left labeled input">
                        <div class="ui selection compact teal basic dropdown label"><!-- compact适应文本 -->
                            <input type="hidden" value="原创" name="flag" th:value="*{flag}">
                            <i class="dropdown icon"></i>
                            <div class="default text m-text-thin">原创</div>
                            <div class="menu">
                                <div class="item" data-value="原创">原创</div>
                                <div class="item" data-value="装载">装载</div>
                                <div class="item" data-value="翻译">翻译</div>
                            </div>
                        </div>
                        <input type="text" name="title" placeholder="标题" th:value="*{title}">
                    </div>
                </div>
                <div class="required field" style="margin-bottom: 0;">
                    <div id="md-content" style="margin-bottom: 0; z-index: 1;">
                        <textarea name="content" placeholder="博客内容" style="display: none;" th:text="*{content}"></textarea>
                    </div>
                </div>
                <div class="required field" style="padding: 0; margin-bottom: 0;">
                    <div class="ui left labeled action input">
                        <label class="ui compact teal basic label">分类</label>
                        <div class="ui fluid selection dropdown">
                            <input type="hidden" name="type.id" th:value="*{type}!=null ? *{type.id}">
                            <i class="dropdown icon"></i>
                            <div class="default text">分类</div>
                            <div class="menu">
                                <div th:each="type : ${types}" th:text="${type.name}" class="item" data-value="1" th:data-value="${type.id}">1</div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="field m-mobile-padded-left" style="padding: 0; margin-bottom: 0;">
                    <div class="ui left labeled action input">
                        <label class="ui compact teal basic label">标签</label>
                        <div class="ui fluid selection multiple search dropdown">
                            <input type="hidden" name="tagIds" th:value="*{tagIds}">
                            <i class="dropdown icon"></i>
                            <div class="default text">标签</div>
                            <div class="menu">
                                <div th:each="tag : ${tags}" th:text="${tag.name}" class="item" data-value="1" th:data-value="${tag.id}">1</div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="required field" style="padding: 0; margin-bottom: 0;">
                    <div class="ui left labeled input">
                        <label class="ui compact teal basic label">首图</label>
                        <input type="text" name="firstPicture" placeholder="首图引用地址" th:value="*{firstPicture}">
                    </div>
                </div>
                <div class="required field">
                    <textarea name="description" th:text="*{description}" placeholder="博客描述..." maxlength="200"></textarea>
                </div>
                <div class="inline fields">
                    <div class="field">
                        <div class="ui checkbox">
                            <input type="checkbox" name="recommend" id="recommend" checked th:checked="*{recommend}" class="hidden">
                            <label for="recommend">推荐</label>
                        </div>
                    </div>
                    <div class="field">
                        <div class="ui checkbox">
                            <input type="checkbox" name="shareStatement" id="shareStatement" th:checked="*{shareStatement}" class="hidden">
                            <label for="shareStatement">转载声明</label>
                        </div>
                    </div>
                    <div class="field">
                        <div class="ui checkbox">
                            <input type="checkbox" name="appreciation" id="appreciation" th:checked="*{appreciation}" class="hidden">
                            <label for="appreciation">赞赏</label>
                        </div>
                    </div>
                    <div class="field">
                        <div class="ui checkbox">
                            <input type="checkbox" name="commentabled" id="commentabled" th:checked="*{commentabled}" class="hidden">
                            <label for="commentabled">评论</label>
                        </div>
                    </div>

                </div>
                <!-- 表单验证时，出错警告 -->
                <div class="ui error message"></div>
                <div class="ui right aligned container">
                    <button type="button" class="ui button" onclick="window.history.go(-1)">返回</button>
                    <div id="save-btn" class="ui secondary button">保存</div>
                    <div id="publish-btn" class="ui teal button">发布</div>
                </div>
            </form>
        </div>
    </div>

    <!-- 底部部分 -->
    <footer th:replace="common/fragment :: footer"></footer>


    <!-- js模块 -->
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>
    <script src="../../static/lib/editormd/editormd.min.js" th:src="@{/lib/editormd/editormd.js}"></script>
    <script src="../../static/js/me.js" th:src="@{/js/me.js}"></script>

    <script>
        // markdown文本编辑器初始化
        var editor;
        $(function () {
            editor = editormd("md-content", {
                width: "100%",
                height: 640,
                syncScrolling: "single",
                emoji: true,
                path: "/lib/editormd/lib/"
            });
        });
        $('.menu.toggle').click(function () {
            $('.m-item').toggleClass('m-mobile-hide'); /* 记住不用加点 */
        });
        // 账号下拉列表
        $('.ui.dropdown').dropdown();
        //表单验证
        $('.ui.form').form({
            fields: {
                title: {
                    identifier: 'title',
                    rules: [{
                        type: 'empty',
                        prompt: '标题：请输入博客标题'
                    }]
                },
                content: {
                    identifier: 'content',
                    rules: [{
                        type: 'empty',
                        prompt: '标题：请输入博客内容'
                    }]
                },
                typeId: {
                    identifier: 'typeId',
                    rules: [{
                        type: 'empty',
                        prompt: '标题：请输入博客分类'
                    }]
                },
                firstPicture: {
                    identifier: 'firstPicture',
                    rules: [{
                        type: 'empty',
                        prompt: '标题：请输入博客首图'
                    }]
                },
                description : {
                    identifier: 'description',
                    rules: [{
                        type : 'empty',
                        prompt: '标题：请输入博客描述'
                    }]
                }
            }
        })
        //点击保存时，默认不发布
        $('#save-btn').on("click", function () {
            $("[name='published']").val(false);
            //console.log("保存按钮执行了");
            $('#blog-form').submit();
        });
        //点击发布，也需要提交表单
        $('#publish-btn').on("click", function () {
            $("[name='published']").val(true);
            //console.log("发布按钮执行了");
            $('#blog-form').submit();
        });
    </script>
</body>

</html>